package cn.wolfcode.service.impl;

import cn.wolfcode.entity.*;
import cn.wolfcode.mapper.StrategyMapper;
import cn.wolfcode.query.StrategyQuery;
import cn.wolfcode.redis.vo.StrategyStatisVO;
import cn.wolfcode.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author c1eye
 * @since 2022-11-03
 */
@Service
public class StrategyServiceImpl extends ServiceImpl<StrategyMapper, Strategy> implements IStrategyService {

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IDestinationService destinationService;


    @Autowired
    private IStrategyContentService strategyContentService;

    @Override
    public boolean saveOrUpdate(Strategy strategy) {
        strategy.setCreateTime(LocalDateTime.now());
        strategy.setViewnum(0);
        strategy.setReplynum(0);
        strategy.setSharenum(0);
        strategy.setFavornum(0);
        strategy.setThumbsupnum(0);
        StrategyCatalog catalog = strategyCatalogService.getById(strategy.getCatalogId());
        strategy.setCatalogName(catalog.getName());
        strategy.setDestId(catalog.getDestId());
        strategy.setDestName(catalog.getDestName());
        StrategyTheme theme = strategyThemeService.getById(strategy.getThemeId());
        strategy.setThemeName(theme.getName());
        List<Destination> toasts = destinationService.queryToasts(strategy.getDestId());
        if (toasts != null && toasts.size() > 0) {
            if (toasts.get(0).getName().equals("中国")) {
                strategy.setIsabroad(0);
            } else {
                strategy.setIsabroad(1);
            }
        }
        // content.content
        save(strategy);
        strategy.getContent().setId(strategy.getId());
        strategyContentService.saveOrUpdate(strategy.getContent());
        return true;
    }

    @Override
    public List<Strategy> getTop3(int destId) {
        QueryWrapper<Strategy> wrapper = new QueryWrapper<>();
        wrapper.eq("dest_id", destId).orderByDesc("viewnum").last("limit 3");
        List<Strategy> list = list(wrapper);
        return list;
    }

    @Override
    public Page<Strategy> selectPage(StrategyQuery qo) {
        Page<Strategy> page = new Page<>();
        page.setCurrent(qo.getCurrentPage());
        page.setSize(qo.getPageSize());
        QueryWrapper<Strategy> wrapper = new QueryWrapper<>();
        wrapper.eq(qo.getDestId() != null, "dest_id", qo.getDestId());
        wrapper.eq(qo.getThemeId() != null, "theme_id", qo.getThemeId());

        if (qo.getType() != null) {
            switch (qo.getType()) {
                case 3: {
                    wrapper.eq(qo.getRefid() != null, "theme_id", qo.getRefid());
                }
                break;
                case 1:
                case 2: {
                    wrapper.eq(qo.getRefid() != null, "dest_id", qo.getRefid());
                }
                break;
            }
        }

        wrapper.orderByDesc(qo.getOrderBy());
        Page<Strategy> list = page(page, wrapper);
        return list;
    }

    @Override
    public List<Map<String, Object>> queryHotTheme() {
        QueryWrapper<Strategy> wrapper = new QueryWrapper<>();
        wrapper.select("theme_name,GROUP_CONCAT(DISTINCT dest_id) ids");
        wrapper.groupBy("theme_name");
        List<Map<String, Object>> maps = listMaps(wrapper);
        ArrayList<Map<String, Object>> res = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            HashMap<String, Object> somap = new HashMap<>();
            String ids = map.get("ids").toString();
            QueryWrapper<Destination> qw = new QueryWrapper<>();
            qw.in("id", ids.split(","));
            List<Destination> des = destinationService.list(qw);
            somap.put("themeName", map.get("theme_name"));
            somap.put("dests", des);
            res.add(somap);
        }

        return res;
    }

    @Override
    public void updateStatisVO(StrategyStatisVO vo) {
        UpdateWrapper<Strategy> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", vo.getStrategyId());
        wrapper.set("viewnum", vo.getViewnum());
        wrapper.set("replynum", vo.getReplynum());
        wrapper.set("favornum", vo.getFavornum());
        wrapper.set("sharenum", vo.getSharenum());
        wrapper.set("thumbsupnum", vo.getThumbsupnum());
        this.update(wrapper);
    }

    @Override
    public List<Strategy> queryByDestId(Long id) {

        QueryWrapper<Strategy> wrapper = new QueryWrapper<>();
        wrapper.eq("dest_id", id);
        List<Strategy> list = list(wrapper);
        return list;
    }
}
